<?php

namespace App\Http\Controllers\Donation;

use App\Enum\CodeEnum;
use App\Http\Controllers\Controller;
use App\Services\DonationService;
use App\Services\DriverService;
use App\Services\VillagerUserService;
use Illuminate\Http\Request;

class UserController extends Controller
{
    /**
     *保存村民信息
     */
    public function updateUserInfo(Request $request)
    {
        try {
            $post = $request->post();
            $res = (new VillagerUserService())->updateUserInfo($post);
            return success($res);
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }
    public function updateUserPhone(Request $request)
    {
        try {
            $post = $request->post();
            $res = (new VillagerUserService())->updateUserPhone($post);
            return success($res);
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     *通过token获取用户信息
     */
    public function getUserInfo(Request $request)
    {
        try {
            $res = (new VillagerUserService())->getUserInfo($request->userId);
            return success($res);
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }
    public function getUserWallet(Request $request)
    {
        try {
            $res = (new VillagerUserService())->getUserWallet($request->userId);
            return success($res);
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * getInfo
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|null
     * @author ninetyseven.
     * @date 2023/4/29
     */
    public function getInfo(Request $request): ?\Illuminate\Http\JsonResponse
    {
        try {
            $driverId = $request->get('driver_id');
            $info = (new DonationService())->getDriverInfo($driverId);
            if(!$info){
                throw new \Exception('获取司机信息失败');
            }
            return success($info);
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }
}
